<?php

namespace App\Http\Controllers\Common;

use App\Http\Controllers\Controller;
use Encore\ImageExt\Utils\ResponseMedia;
use Encore\ImageExt\Services\MediaService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;


class MediaController extends Controller
{
    protected $mediaService;

    public function __construct(MediaService $mediaService)
    {
        $this->mediaService = $mediaService;
    }

    public function upload(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'file' => 'required'
        ]);

        if ($validator->fails()) {
            return  $validator->errors()->first();
        }
        $move = new \stdClass();
        $move->dir = $request->get('dir', 'user');
        $move->fileNameIsEncrypt = $request->get('encrypt', false);

        $media_obj = $this->mediaService->upload(
            Auth::id()?:$request->get('uid', 0),
             $request->file('file'),
            $move,
            'user'
        );
        return ResponseMedia::make($media_obj);
    }
}
